package com.huawei.struts.service.imp;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import com.huawei.struts.dao.UserTDAO;
import com.huawei.struts.exception.SystemException;
import com.huawei.struts.service.UserTService;
import com.huawei.struts.util.PageBean;
import com.st.model.UserT;

public class UserTImpl implements UserTService {

	

	private UserTDAO userTDAO;

	public void setUserTDAO(UserTDAO userTDAO) {
		this.userTDAO = userTDAO;
	}

	public List export(UserT user) {
		 
		return userTDAO.export(user);
	}
	public boolean addUser(UserT user) throws SystemException {
		boolean flag = false;
		try {
			userTDAO.save(user);
			flag = true;
		} catch (Exception e) {
			throw new SystemException("error.info", "数据库异常"+e.getCause());
		}
		return flag;

	}

	public PageBean queryForPage(UserT user,PageBean pageBean1) {
		final String hql = "from UserT";
		// 查询语句
		int allRow = Integer.parseInt(userTDAO.getAllRowCount(user).toString());
		int pageSize=pageBean1.getPageSize();
		int currentPage=pageBean1.getCurrentPage();
		// 总记录数
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		
		// 总页数
		final int offset = PageBean.countOffset(pageSize, currentPage, totalPage);
		// 当前页开始记录
		final int length = pageSize;
		// 每页记录数
		List list = userTDAO.queryForPage(offset, length, user);
		
		// 把分页信息保存到bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(PageBean.CorrectCurrentPage(currentPage, totalPage));
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.setUpPage(PageBean.CorrectCurrentPage(currentPage, totalPage)-1);
		pageBean.setDownPage(PageBean.CorrectCurrentPage(currentPage, totalPage)+1);
		return pageBean;
	}

	public UserT getUserById(Integer id) {
		return userTDAO.findById(id);
	}

	/**
	 * 登陆
	 */
	public boolean login(UserT user) throws SystemException {
		return userTDAO.login(user);
	}

	/**
	 * delete user by id
	 */

	public boolean delete(UserT user) throws SystemException {
		boolean flag = false;
		try {
			userTDAO.delete(user);
			flag = true;
		} catch (Exception ex) {
			throw new SystemException("error.info", "数据库异常");
		}
		return flag;
	}

	/**
	 * merge user by id
	 */
	public boolean modify(UserT user) throws SystemException {

		boolean flag = false;
		try {
			userTDAO.merge(user);
			flag = true;
		} catch (Exception ex) {
			throw new SystemException("error.info", "数据库异常");
		}
		return flag;
	}

	public UserT getUserByName(String name) throws SystemException {
		UserT user = new UserT();
		List list = userTDAO.findBySname(name);
		if (list != null && list.size() > 0) {
			user = (UserT) list.get(0);
		}
		return user;
	}
	
	public int getTotalCount(UserT user)
	{
		int allRow = Integer.parseInt(userTDAO.getAllRowCount(user).toString());
		return allRow;
	}
	
	public boolean importExcelData(String path) throws SystemException
	{
		boolean flag=false;
		File file=new File(path);
		Workbook book=null;
		try {
			book= Workbook.getWorkbook(file);
			 //获得第一个工作表对象 
			 Sheet sheet=book.getSheet(0); 
			 //得到第一列第一行的单元格 
			 int rows=sheet.getRows();
			 for(int i=1;i<rows;i++)
			 {
				 UserT user=new UserT();
				 user.setSname(sheet.getCell(0,i).getContents());
				 user.setSpassword(sheet.getCell(1,i).getContents());
				 userTDAO.save(user);
				 flag=true;
			 }
			  
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		finally
		{
			book.close();
			if(file.exists())
			{
				file.delete();
			}
		}
		
		return flag;
	}

}
